示例程序

    这里列出了所有的示例程序:

ChunkData.cpp获取Chunk信息
ConnectSpecCamera.cpp 通过ip地址连接相机
Display.cpp 图像显示
Events.cpp 获取相机事件
ForceIP.cpp 设置相机静态ip
Grab_ImageCallback.cpp 通过回调获取相机图像
GrabImage.cpp 获取相机图像
GrabImage_HighPerformance.cpp 高性能获取相机图像
GrabMultipleCamera.cpp 多相机取流
ImageProcess.cpp 图像处理
MultiCast.cpp 设置组播模式
ParametrizeCamera_FileAccess.cpp 导入或导出文件到相机
ParametrizeCamera_LoadAndSave.cpp     导入或导出相机属性
ReconnectDemo.cpp 相机重连
SetIO.cpp 设置相机IO
SetParam.cpp 设置相机参数
Trigger_Image.cpp 触发模式获取相机图像
Trigger_ImageCallback.cpp 触发模式下通过回调获取相机图像


ChunkData.cpp

获取Chunk信息

该示例程序说明了如何打开ChunkData功能、如何配置ChunkData以及如何获取这些ChunkData信息。ChunkData例如Timestamp 、Exposure 等。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(pUser, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * stParam.nCurValue);
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize = stParam.nCurValue;
while(1)
{
if(g_bExit)
{
break;
}
nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
//Print parse the timestamp information in the frame
printf("Get One Frame: Chunk_ExposureTime[%f], Chunk_SecondCount[%d], Chunk_CycleCount[%d], Chunk_CycleOffset[%d], nFrameNum[%d]\n",
stImageInfo.fExposureTime, stImageInfo.nSecondCount, stImageInfo.nCycleCount, stImageInfo.nCycleOffset, stImageInfo.nFrameNum);
}
else{
printf("No data[%x]\n", nRet);
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// ch:开启Chunk Mode | en:Open Chunk Mode
nRet = MV_CC_SetBoolValue(handle, "ChunkModeActive", true);
if (MV_OK != nRet)
{
printf("Set Chunk Mode fail! nRet [0x%x]\n", nRet);
break;
}
// ch:Chunk Selector设为Exposure | en: Chunk Selector set as Exposure
nRet = MV_CC_SetEnumValueByString(handle, "ChunkSelector", "Exposure");
if (MV_OK != nRet)
{
printf("Set Exposure Chunk fail! nRet [0x%x]\n", nRet);
break;
}
// ch:开启Chunk Enable | en:Open Chunk Enable
nRet = MV_CC_SetBoolValue(handle, "ChunkEnable", true);
if (MV_OK != nRet)
{
printf("Set Chunk Enable fail! nRet [0x%x]\n", nRet);
break;
}
// ch:Chunk Selector设为Timestamp | en: Chunk Selector set as Timestamp
nRet = MV_CC_SetEnumValueByString(handle, "ChunkSelector", "Timestamp");
if (MV_OK != nRet)
{
printf("Set Timestamp Chunk fail! nRet [0x%x]\n", nRet);
break;
}
// ch:开启Chunk Enable | en:Open Chunk Enable
nRet = MV_CC_SetBoolValue(handle, "ChunkEnable", true);
if (MV_OK != nRet)
{
printf("Set Chunk Enable fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
// 设置触发模式为off
// set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
// 停止取流
// end grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}

ConnectSpecCamera.cpp

通过ip地址连接相机

该示例程序说明了如何通过ip地址连接网口相机。通过输入需要连接的相机ip(Camera Ip)和相机对应的网卡ip(Export Ip)来连接相机,输入的格式为xx.xx.xx.xx。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * (g_nPayloadSize));
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize = g_nPayloadSize;
while(1)
{
if(g_bExit)
{
break;
}
nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else
{
printf("No data[0x%x]\n", nRet);
break;
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
MV_CC_DEVICE_INFO stDevInfo = {0};
MV_GIGE_DEVICE_INFO stGigEDev = {0};
// ch:需要连接的相机ip(根据实际填充) | en:The camera IP that needs to be connected (based on actual padding)
printf("Please input Current Camera Ip : ");
char nCurrentIp[128];
scanf("%s", &nCurrentIp);
// ch:相机对应的网卡ip(根据实际填充) | en:The pc IP that needs to be connected (based on actual padding)
printf("Please input Net Export Ip : ");
char nNetExport[128];
scanf("%s", &nNetExport);
unsigned int nIp1, nIp2, nIp3, nIp4, nIp;
sscanf(nCurrentIp, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
stGigEDev.nCurrentIp = nIp;
sscanf(nNetExport, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
stGigEDev.nNetExport = nIp;
stDevInfo.nTLayerType = MV_GIGE_DEVICE;// ch:仅支持GigE相机 | en:Only support GigE camera
stDevInfo.SpecialInfo.stGigEInfo = stGigEDev;
do
{
// ch:选择设备并创建句柄 | en:Select device and create handle
nRet = MV_CC_CreateHandle(&handle, &stDevInfo);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet[0x%x]\n", nRet);
break;
}
// ch:打开设备 | en:Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
// ch:设置触发模式为off | en:Set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
g_nPayloadSize = stParam.nCurValue;
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
printf("Press a key to stop grabbing.\n");
PressEnterToExit();
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | en:Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("Close Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | en:Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}

Display.cpp

图像显示

演示了图像采集并使用图像显示接口进行显示。

#include <X11/Xlib.h>
#include <assert.h>
#include "math.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define NIL (0)
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
Window w;
Display *dpy;
memset(&w, 0, sizeof(Window));
dpy = NULL;
// 打开连接到X服务器的连接
// open the connection to the display 0
dpy = XOpenDisplay(NIL);
if (NULL == dpy)
{
printf("please run with screan environment\n");
return -1;
}
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
752, 480, 0, 0xffff00ff, 0xff00ffff);
// 获取改变窗口大小事件
// we want to get MapNotify events
XSelectInput(dpy, w, StructureNotifyMask);
// 使窗口可见
// "Map" the window (that is, make it appear on the screen)
XMapWindow(dpy, w);
// 创建图像上下文给出绘图函数的属性
// Create a "Graphics Context"
GC gc = XCreateGC(dpy, w, 0, NIL);
// 告诉GC使用白色
// Tell the GC we draw using the white color
XSetForeground(dpy, gc, whiteColor);
// 等待事件的到来
// Wait for the MapNotify event
for(;;)
{
XEvent e;
XNextEvent(dpy, &e);
if (e.type == MapNotify)
{
break;
}
}
int nRet = MV_OK;
void* handle = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
nRet= MV_CC_Display(handle, (void*)w);
if (MV_OK != nRet)
{
printf("MV_CC_Display fail! nRet [%x]\n", nRet);
break;
}
printf("Display succeed\n");
PressEnterToExit();
// 停止取流
// stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
}while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}

Events.cpp

获取相机事件

该示例程序说明了如何配置相机事件功能、如何注册事件回调以及如何在事件回调函数中处理获取到的事件信息。Event事件例如:ExposureEnd。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
void __stdcall EventCallBack(MV_EVENT_OUT_INFO * pEventInfo, void* pUser)
{
if (pEventInfo)
{
int64_t nBlockId = pEventInfo->nBlockIdHigh;
nBlockId = (nBlockId << 32) + pEventInfo->nBlockIdLow;
int64_t nTimestamp = pEventInfo->nTimestampHigh;
nTimestamp = (nTimestamp << 32) + pEventInfo->nTimestampLow;
printf("EventName[%s], EventID[%d], BlockId[%lld], Timestamp[%lld]\n",
pEventInfo->EventName, pEventInfo->nEventID, nBlockId, nTimestamp);
}
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// ch:枚举设备 | en:Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// ch:选择设备并创建句柄 | en:Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// ch:打开设备 | en:Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
// ch:开启Event | en:Set Event of ExposureEnd On
nRet = MV_CC_SetEnumValueByString(handle,"EventSelector","ExposureEnd");
if (MV_OK != nRet)
{
printf("Set Event Selector fail! nRet [0x%x]\n", nRet);
break;
}
nRet = MV_CC_SetEnumValueByString(handle,"EventNotification","On");
if (MV_OK != nRet)
{
printf("Set Event Notification fail! nRet [0x%x]\n", nRet);
break;
}
// ch:注册事件回调 | en:Register event callback
nRet = MV_CC_RegisterEventCallBackEx(handle, "ExposureEnd", EventCallBack, handle);
if (MV_OK != nRet)
{
printf("Register Event CallBack fail! nRet [0x%x]\n", nRet);
break;
}
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
PressEnterToExit();
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | en:Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("Close Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | en:Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}

ForceIP.cpp

设置相机静态ip

该示例程序说明了如何设置网口相机静态ip。通过输入相机ip,子网掩码(NetMask),默认网关(DefaultWay)来设置相机ip地址。输入的格式为xx.xx.xx.xx。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool ConvertToHexIp(unsigned int *nHexIP, unsigned int *nDecIP, char c)
{
if ( nDecIP[0] < 0 || nDecIP[0] > 255
|| nDecIP[1] < 0 || nDecIP[1] > 255
|| nDecIP[2] < 0 || nDecIP[2] > 255
|| nDecIP[3] < 0 || nDecIP[3] > 255
|| c != '\n')
{
return false;
}
*nHexIP = (nDecIP[0] << 24) + (nDecIP[1] << 16) + (nDecIP[2] << 8) + nDecIP[3];
return true;
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
unsigned int nIP[4] = {0};
char c = '\0';
unsigned int nIpAddr = 0, nNetWorkMask = 0, nDefaultGateway = 0;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE, &stDeviceList);
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 输入IP 子网掩码 默认网关
// input ip, subnet mask and defaultway
printf("Please input ip, example: 192.168.1.100\n");
int ch;
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nIpAddr, nIP, c))
{
printf("input IpAddr format is not correct\n");
break;
}
printf("Please input NetMask, example: 255.255.255.0\n");
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nNetWorkMask, nIP, c))
{
printf("input NetMask format is not correct\n");
break;
}
printf("Please input DefaultWay, example: 192.168.1.1\n");
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nDefaultGateway, nIP, c))
{
printf("input DefaultWay format is not correct\n");
break;
}
// 设置ForceIP
// set forceip
nRet = MV_GIGE_ForceIpEx(handle, nIpAddr, nNetWorkMask, nDefaultGateway);
if (MV_OK != nRet)
{
printf("MV_GIGE_ForceIpEx fail! nRet [%x]\n", nRet);
break;
}
printf("set IP succeed\n");
PressEnterToExit();
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}

Grab_ImageCallback.cpp

通过回调获取相机图像

该示例程序说明了如何通过回调的方式获取相机图像。创建句柄,打开相机,注册回调函数,开始取流,在回调函数中可以获取到图像相关信息,停止取流,关闭相机,销毁句柄。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (pFrameInfo)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
pFrameInfo->nWidth, pFrameInfo->nHeight, pFrameInfo->nFrameNum);
}
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
// 设置触发模式为off
// set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
// 注册抓图回调
// register image callback
nRet = MV_CC_RegisterImageCallBackEx(handle, ImageCallBackEx, handle);
if (MV_OK != nRet)
{
printf("MV_CC_RegisterImageCallBackEx fail! nRet [%x]\n", nRet);
break;
}
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
PressEnterToExit();
// 停止取流
// end grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}

GrabImage.cpp

获取相机图像

该示例程序说明了如何通过主动取图的方式获取相机图像。创建句柄,打开相机,开始取流,创建取流线程(用户分配采集buffer, MV_CC_GetOneFrameTimeout() 取图(拷贝)),停止取流,关闭相机,销毁句柄。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(pUser, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * stParam.nCurValue);
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize = stParam.nCurValue;
while(1)
{
if(g_bExit)
{
break;
}
nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else{
printf("No data[%x]\n", nRet);
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
// 设置触发模式为off
// set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
// 停止取流
// end grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}

GrabImage_HighPerformance.cpp

高性能获取相机图像

该示例程序说明了高效率的主动取图方式。创建句柄,打开相机,开始取流,创建取流线程( MV_CC_GetImageBuffer() 获取图像地址, MV_CC_FreeImageBuffer() 释放图像缓存),停止取流,关闭相机,销毁句柄。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
// ch:等待按键输入 | en:Wait for key press
void WaitForKeyPress(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT stOutFrame = {0};
memset(&stOutFrame, 0, sizeof(MV_FRAME_OUT));
while(1)
{
nRet = MV_CC_GetImageBuffer(pUser, &stOutFrame, 1000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stOutFrame.stFrameInfo.nWidth, stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nFrameNum);
}
else
{
printf("No data[0x%x]\n", nRet);
}
if(NULL != stOutFrame.pBufAddr)
{
nRet = MV_CC_FreeImageBuffer(pUser, &stOutFrame);
if(nRet != MV_OK)
{
printf("Free Image Buffer fail! nRet [0x%x]\n", nRet);
}
}
if(g_bExit)
{
break;
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// ch:枚举设备 | en:Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index:");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// ch:选择设备并创建句柄 | en:Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// ch:打开设备 | en:Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
}
}
// ch:设置触发模式为off | en:Set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
g_nPayloadSize = stParam.nCurValue;
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
printf("Press a key to stop grabbing.\n");
WaitForKeyPress();
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("Press a key to exit.\n");
WaitForKeyPress();
return 0;
}

GrabMultipleCamera.cpp

多相机取流

该示例程序说明了如何实现多相机取图。示例中通过创建多个相机句柄handle的方式以区分相机。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define CAMERA_NUM 2
bool g_bExit = false;
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
MVCC_STRINGVALUE stStringValue = {0};
char camSerialNumber[256] = {0};
nRet = MV_CC_GetStringValue(pUser, "DeviceSerialNumber", &stStringValue);
if (MV_OK == nRet)
{
memcpy(camSerialNumber, stStringValue.chCurValue, sizeof(stStringValue.chCurValue));
}
else
{
printf("Get DeviceUserID Failed! nRet = [%x]\n", nRet);
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(pUser, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * stParam.nCurValue);
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize = stParam.nCurValue;
while(1)
{
if(g_bExit)
{
break;
}
nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Cam Serial Number[%s]:GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
camSerialNumber, stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else
{
printf("cam[%s]:Get One Frame failed![%x]\n", camSerialNumber, nRet);
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle[CAMERA_NUM] = {NULL};
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
return -1;
}
unsigned int nIndex = 0;
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
return -1;
}
if(stDeviceList.nDeviceNum < CAMERA_NUM)
{
printf("only have %d camera\n", stDeviceList.nDeviceNum);
return -1;
}
// 提示为多相机测试
// Tips for multicamera testing
printf("Start %d camera Grabbing Image test\n", CAMERA_NUM);
for(int i = 0; i < CAMERA_NUM; i++)
{
printf("Please Input Camera Index: ");
scanf("%d", &nIndex);
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle[i], stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
MV_CC_DestroyHandle(handle[i]);
return -1;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle[i]);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
MV_CC_DestroyHandle(handle[i]);
return -1;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle[i]);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle[i],"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
}
for(int i = 0; i < CAMERA_NUM; i++)
{
// 设置触发模式为off
// set trigger mode as off
nRet = MV_CC_SetEnumValue(handle[i], "TriggerMode", MV_TRIGGER_MODE_OFF);
if (MV_OK != nRet)
{
printf("Cam[%d]: MV_CC_SetTriggerMode fail! nRet [%x]\n", i, nRet);
}
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(handle[i]);
if (MV_OK != nRet)
{
printf("Cam[%d]: MV_CC_StartGrabbing fail! nRet [%x]\n",i, nRet);
return -1;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle[i]);
if (nRet != 0)
{
printf("Cam[%d]: thread create failed.ret = %d\n",i, nRet);
return -1;
}
}
PressEnterToExit();
for(int i = 0; i < CAMERA_NUM; i++)
{
// 停止取流
// end grab image
nRet = MV_CC_StopGrabbing(handle[i]);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
return -1;
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle[i]);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
return -1;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle[i]);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
return -1;
}
}
printf("exit\n");
return 0;
}

ImageProcess.cpp

图像处理

该示例程序说明了如何实现图像像素格式转换和图像保存两个功能。在获取到图像数据后可以使用 MV_CC_ConvertPixelType() 进行数据格式转换,使用 MV_CC_SaveImageEx2() 进行图像保存。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
unsigned char * pData = NULL;
unsigned char *pDataForRGB = NULL;
unsigned char *pDataForSaveImage = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
pData = (unsigned char *)malloc(sizeof(unsigned char) * stParam.nCurValue);
if (NULL == pData)
{
break;
}
unsigned int nDataSize = stParam.nCurValue;
nRet = MV_CC_GetOneFrameTimeout(handle, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Now you GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
// 处理图像
// image processing
printf("input 0 to do nothing, 1 to convert RGB, 2 to save as BMP\n");
int nInput = 0;
scanf("%d", &nInput);
switch (nInput)
{
// 不做任何事,继续往下走
// do nothing, and go on next
case 0:
{
break;
}
// 转换图像为RGB格式,用户可根据自身需求转换其他格式
// convert image format to RGB, user can convert to other format by their requirement
case 1:
{
pDataForRGB = (unsigned char*)malloc(stImageInfo.nWidth * stImageInfo.nHeight * 4 + 2048);
if (NULL == pDataForRGB)
{
break;
}
// 像素格式转换
// convert pixel format
MV_CC_PIXEL_CONVERT_PARAM stConvertParam = {0};
// 从上到下依次是:图像宽,图像高,输入数据缓存,输入数据大小,源像素格式,
// 目标像素格式,输出数据缓存,提供的输出缓冲区大小
// Top to bottom are:image width, image height, input data buffer, input data size, source pixel format,
// destination pixel format, output data buffer, provided output buffer size
stConvertParam.nWidth = stImageInfo.nWidth;
stConvertParam.nHeight = stImageInfo.nHeight;
stConvertParam.pSrcData = pData;
stConvertParam.nSrcDataLen = stImageInfo.nFrameLen;
stConvertParam.enSrcPixelType = stImageInfo.enPixelType;
stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed;
stConvertParam.pDstBuffer = pDataForRGB;
stConvertParam.nDstBufferSize = stImageInfo.nWidth * stImageInfo.nHeight * 4 + 2048;
nRet = MV_CC_ConvertPixelType(handle, &stConvertParam);
if (MV_OK != nRet)
{
printf("MV_CC_ConvertPixelType fail! nRet [%x]\n", nRet);
break;
}
FILE* fp = fopen("AfterConvert_RGB.raw", "wb");
if (NULL == fp)
{
printf("fopen failed\n");
break;
}
fwrite(pDataForRGB, 1, stConvertParam.nDstLen, fp);
fclose(fp);
printf("convert succeed\n");
break;
}
case 2:
{
pDataForSaveImage = (unsigned char*)malloc(stImageInfo.nWidth * stImageInfo.nHeight * 4 + 2048);
if (NULL == pDataForSaveImage)
{
break;
}
// 填充存图参数
// fill in the parameters of save image
memset(&stSaveParam, 0, sizeof(MV_SAVE_IMAGE_PARAM_EX));
// 从上到下依次是:输出图片格式,输入数据的像素格式,提供的输出缓冲区大小,图像宽,
// 图像高,输入数据缓存,输出图片缓存,JPG编码质量
// Top to bottom are:
stSaveParam.enImageType = MV_Image_Bmp;
stSaveParam.enPixelType = stImageInfo.enPixelType;
stSaveParam.nBufferSize = stImageInfo.nWidth * stImageInfo.nHeight * 4 + 2048;
stSaveParam.nWidth = stImageInfo.nWidth;
stSaveParam.nHeight = stImageInfo.nHeight;
stSaveParam.pData = pData;
stSaveParam.nDataLen = stImageInfo.nFrameLen;
stSaveParam.pImageBuffer = pDataForSaveImage;
stSaveParam.nJpgQuality = 80;
nRet = MV_CC_SaveImageEx2(handle, &stSaveParam);
if(MV_OK != nRet)
{
printf("failed in MV_CC_SaveImage,nRet[%x]\n", nRet);
break;
}
FILE* fp = fopen("image.bmp", "wb");
if (NULL == fp)
{
printf("fopen failed\n");
break;
}
fwrite(pDataForSaveImage, 1, stSaveParam.nImageLen, fp);
fclose(fp);
printf("save image succeed\n");
break;
}
default:
break;
}
}
// 停止取流
// end grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
if (pData)
{
free(pData);
pData = NULL;
}
if (pDataForRGB)
{
free(pDataForRGB);
pDataForRGB = NULL;
}
if (pDataForSaveImage)
{
free(pDataForSaveImage);
pDataForSaveImage = NULL;
}
PressEnterToExit();
printf("exit\n");
return 0;
}

MultiCast.cpp

设置组播模式

该示例程序说明了如何设置传输模式为组播模式。通过询问用户启动多播控制应用程序或者多播监控应用程序,然后根据用户输入,以对应的访问权限打开相机,最后通过 MV_GIGE_SetTransmissionType() 设置组播模式。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * (g_nPayloadSize));
if (pData == NULL)
{
return 0;
}
unsigned int nDataSize = g_nPayloadSize;
while(1)
{
nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else
{
printf("No data[0x%x]\n", nRet);
}
if(g_bExit)
{
break;
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// ch:枚举设备 |en:Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// ch:选择设备并创建句柄 | en:select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
int c;
while ( (c = getchar()) != '\n' && c != EOF );
char key;
// ch:询问用户启动多播控制应用程序或多播监控应用程序
// en:Ask the user to launch: the multicast controlling application or the multicast monitoring application.
printf("Start multicast sample in (c)ontrol or in (m)onitor mode? (c/m)\n");
scanf("%c", &key);
if((key != 'c') && (key != 'm') && (key != 'C') && (key != 'M'))
{
printf("Input error\n");
break;
}
// ch:查询用户使用的模式 | en:Query the user for the mode to use.
bool monitorMode = (key == 'm') || (key == 'M');
// ch:打开设备 | Open device
if (monitorMode)
{
}
else
{
}
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (MV_GIGE_DEVICE == stDeviceList.pDeviceInfo[nIndex]->nTLayerType && false == monitorMode)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
g_nPayloadSize = stParam.nCurValue;
// ch:指定组播ip | en:multicast IP
char strIp[] = "239.192.1.1";
unsigned int nIp1, nIp2, nIp3, nIp4, nIp;
sscanf(strIp, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
// ch:可指定端口号作为组播组端口 | en:multicast port
MV_TRANSMISSION_TYPE stTransmissionType;
memset(&stTransmissionType, 0, sizeof(MV_TRANSMISSION_TYPE));
stTransmissionType.nDestIp = nIp;
stTransmissionType.nDestPort = 1042;
nRet = MV_GIGE_SetTransmissionType(handle, &stTransmissionType);
if (MV_OK != nRet)
{
printf("Set Transmission Type fail! nRet [0x%x]\n", nRet);
break;
}
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
pthread_t nThreadID = 0;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
break;
}
printf("Press a key to stop grabbing.\n");
PressEnterToExit();
g_bExit = true;
sleep(1);
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | en:Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | en:Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}

ParametrizeCamera_FileAccess.cpp

导入或导出文件到相机

该示例程序说明了如何导入文件到相机以及如何导出相机文件。演示了通过 MV_CC_FileAccessRead() 从设备读取文件(例如UserSet1.bin);通过 MV_CC_FileAccessWrite() 将文件(例如UserSet1.bin)写入相机;以及通过 MV_CC_GetFileAccessProgress() 获取文件的存取进度。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
unsigned int g_nMode = 0;
int g_nRet = MV_OK;
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
static void* ProgressThread(void* pUser)
{
int nRet = MV_OK;
MV_CC_FILE_ACCESS_PROGRESS stFileAccessProgress = {0};
while(1)
{
//ch:获取文件存取进度 |en:Get progress of file access
nRet = MV_CC_GetFileAccessProgress(pUser, &stFileAccessProgress);
printf("State = 0x%x,Completed = %ld,Total = %ld\r\n",nRet,stFileAccessProgress.nCompleted,stFileAccessProgress.nTotal);
if (nRet != MV_OK || (stFileAccessProgress.nCompleted != 0 && stFileAccessProgress.nCompleted == stFileAccessProgress.nTotal))
{
break;
}
usleep(50000);
}
return 0;
}
static void* FileAccessThread(void* pUser)
{
MV_CC_FILE_ACCESS stFileAccess = {0};
stFileAccess.pUserFileName = "UserSet1.bin";
stFileAccess.pDevFileName = "UserSet1";
if (1 == g_nMode)
{
//ch:读模式 |en:Read mode
g_nRet = MV_CC_FileAccessRead(pUser, &stFileAccess);
if (MV_OK != g_nRet)
{
printf("File Access Read fail! nRet [0x%x]\n", g_nRet);
}
}
else if (2 == g_nMode)
{
//ch:写模式 |en:Write mode
g_nRet = MV_CC_FileAccessWrite(pUser, &stFileAccess);
if (MV_OK != g_nRet)
{
printf("File Access Write fail! nRet [0x%x]\n", g_nRet);
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// ch:枚举设备 | en:Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// ch:选择设备并创建句柄 | en:Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// ch:打开设备 | en:Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
//ch:读模式 |en:Read mode
g_nMode = 1;
printf("Read to file.\n");
pthread_t nReadHandle;
nRet = pthread_create(&nReadHandle, NULL ,FileAccessThread , handle);
if (nRet != 0)
{
break;
}
usleep(5000);
pthread_t nReadProcessHandle;
nRet = pthread_create(&nReadProcessHandle, NULL ,ProgressThread , handle);
if (nRet != 0)
{
break;
}
void *statusRead;
void *statusReadProcess;
pthread_join(nReadHandle, &statusRead);
pthread_join(nReadProcessHandle, &statusReadProcess);
if (MV_OK == g_nRet)
{
printf("File Access Read Success!\n");
}
printf("\n");
//ch:写模式 |en:Write mode
g_nMode = 2;
printf("Write from file.\n");
pthread_t nWriteHandle;
nRet = pthread_create(&nWriteHandle, NULL ,FileAccessThread , handle);
if (nRet != 0)
{
break;
}
usleep(5000);
pthread_t nWriteProgressHandle;
nRet = pthread_create(&nWriteProgressHandle, NULL ,ProgressThread , handle);
if (nRet != 0)
{
break;
}
void *statusWrite;
void *statusWriteProcess;
pthread_join(nWriteHandle, &statusWrite);
pthread_join(nWriteProgressHandle, &statusWriteProcess);
if (MV_OK == g_nRet)
{
printf("File Access Write Success!\n");
}
// ch:关闭设备 | Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}

ParametrizeCamera_LoadAndSave.cpp    

导入或导出相机属性

该示例程序说明了如何导入和导出相机的属性树文件。演示了通过 MV_CC_FeatureSave() 将相机属性导出到文件(例如FeatureFile.ini)中;通过 MV_CC_FeatureLoad() 从文件(例如FeatureFile.ini)中导入相机的属性树。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// ch:枚举设备 | en:Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// ch:选择设备并创建句柄 | en:Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// ch:打开设备 | en:Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
printf("Start export the camera properties to the file\n");
printf("Wait......\n");
// ch:将相机属性导出到文件中 | en:Export the camera properties to the file
nRet = MV_CC_FeatureSave(handle, "FeatureFile.ini");
if (MV_OK != nRet)
{
printf("Save Feature fail! nRet [0x%x]\n", nRet);
break;
}
printf("Finish export the camera properties to the file\n\n");
printf("Start import the camera properties from the file\n");
printf("Wait......\n");
// ch:从文件中导入相机属性 | en:Import the camera properties from the file
nRet = MV_CC_FeatureLoad(handle, "FeatureFile.ini");
if (MV_OK != nRet)
{
printf("Load Feature fail! nRet [0x%x]\n", nRet);
break;
}
printf("Finish import the camera properties from the file\n");
// ch:关闭设备 | Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}

ReconnectDemo.cpp

相机重连

该示例程序说明了相机掉线后,心跳时间内重新连接后的操作,通过在打开相机后注册异常回调函数来监测相机掉线异常状态。重连后先进行停止取流、关闭设备、销毁句柄操作后重新连接相机。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void* g_hHandle = NULL;
bool g_bConnect = false;
char g_strSerialNumber[64] = {0};
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
void __stdcall cbException(unsigned int nMsgType, void* pUser)
{
printf("Device disconnect!\n");
g_bConnect = false;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(g_hHandle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * stParam.nCurValue);
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize = stParam.nCurValue;
while(1)
{
if(!g_bConnect)
{
break;
}
nRet = MV_CC_GetOneFrameTimeout(g_hHandle, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else
{
printf("no data[%x]\n", nRet);
}
}
free(pData);
return 0;
}
static void* ReconnectProcess(void* pUser)
{
int nRet = MV_OK;
MV_CC_DEVICE_INFO_LIST stDeviceList = {0};
while(1)
{
if (true == g_bConnect)
{
sleep(1);
continue;
}
nRet = MV_CC_StopGrabbing(g_hHandle);
nRet = MV_CC_CloseDevice(g_hHandle);
nRet = MV_CC_DestroyHandle(g_hHandle);
g_hHandle = NULL;
printf("connecting...\n");
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
continue;
}
// 根据序列号选择相机
unsigned int nIndex = -1;
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
continue;
}
if (pDeviceInfo->nTLayerType == MV_GIGE_DEVICE)
{
if (!strcmp((char*)(pDeviceInfo->SpecialInfo.stGigEInfo.chSerialNumber), g_strSerialNumber))
{
nIndex = i;
break;
}
}
else if (pDeviceInfo->nTLayerType == MV_USB_DEVICE)
{
if (!strcmp((char*)(pDeviceInfo->SpecialInfo.stUsb3VInfo.chSerialNumber), g_strSerialNumber))
{
nIndex = i;
break;
}
}
}
}
else
{
continue;
}
if (-1 == nIndex)
{
continue;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&g_hHandle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
continue;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(g_hHandle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
continue;
}
g_bConnect = true;
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(g_hHandle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(g_hHandle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
// 注册异常回调
// register exception callback
nRet = MV_CC_RegisterExceptionCallBack(g_hHandle, cbException, NULL);
if (MV_OK != nRet)
{
printf("MV_CC_RegisterExceptionCallBack fail! nRet [%x]\n", nRet);
continue;
}
printf("connect succeed\n");
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(g_hHandle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
continue;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , NULL);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
continue;
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
MV_CC_DEVICE_INFO_LIST stDeviceList = {0};
unsigned int nSelectNum = 0;
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
return -1;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
return -1;
}
printf("Please Intput camera index: ");
scanf("%d", &nSelectNum);
if (nSelectNum >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
return -1;
}
if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_GIGE_DEVICE)
{
memcpy(g_strSerialNumber, stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stGigEInfo.chSerialNumber,
sizeof(stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stGigEInfo.chSerialNumber));
}
else if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_USB_DEVICE)
{
memcpy(g_strSerialNumber, stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stUsb3VInfo.chSerialNumber,
sizeof(stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stUsb3VInfo.chSerialNumber));
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL, ReconnectProcess, NULL);
if (nRet != 0)
{
printf("thread create failed nRet = %d\n",nRet);
return -1;
}
PressEnterToExit();
g_bConnect = false;
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(g_hHandle);
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(g_hHandle);
printf("exit\n");
return 0;
}

SetIO.cpp

设置相机IO

该示例程序说明了在打开设备后如何设置和获取设备IO状态。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// 获取LineSelector
// get LineSelector
MVCC_ENUMVALUE stLineSelector = {0};
nRet = MV_CC_GetEnumValue(handle, "LineSelector", &stLineSelector);
if (MV_OK == nRet)
{
printf("stLineSelector current value:%d\n", stLineSelector.nCurValue);
printf("supported stLineSelector number:%d\n", stLineSelector.nSupportedNum);
for (unsigned int i = 0; i < stLineSelector.nSupportedNum; ++i)
{
printf("supported stLineSelector [%d]:%d\n", i, stLineSelector.nSupportValue[i]);
}
printf("\n");
}
else
{
printf("get stLineSelector failed! nRet [%x]\n\n", nRet);
}
// 设置LineSelector
// set LineSelector
unsigned int nLineSelector = 0;
printf("please input the LineSelector to set: ");
scanf("%d", &nLineSelector);
nRet = MV_CC_SetEnumValue(handle, "LineSelector", nLineSelector);
if (MV_OK == nRet)
{
printf("set LineSelector OK!\n\n");
}
else
{
printf("set LineSelector failed! nRet [%x]\n\n", nRet);
}
// 获取LineMode
// get LineMode
MVCC_ENUMVALUE stLineMode = {0};
nRet = MV_CC_GetEnumValue(handle, "LineMode", &stLineMode);
if (MV_OK == nRet)
{
printf("stLineMode current value:%d\n", stLineMode.nCurValue);
printf("supported stLineSelector number:%d\n", stLineMode.nSupportedNum);
for (unsigned int i = 0; i < stLineMode.nSupportedNum; ++i)
{
printf("supported stLineSelector [%d]:%d\n", i, stLineMode.nSupportValue[i]);
}
printf("\n");
}
else
{
printf("get stLineMode failed! nRet [%x]\n\n", nRet);
}
// 设置LineMode
// set LineMode
unsigned int nLineMode = 0;
printf("please input the LineMode to set:");
scanf("%d", &nLineMode);
nRet = MV_CC_SetEnumValue(handle, "LineMode", nLineMode);
if (MV_OK == nRet)
{
printf("set LineMode OK!\n\n");
}
else
{
printf("set LineMode failed! nRet [%x]\n\n", nRet);
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}

SetParam.cpp

设置相机参数

该示例程序说明了如何使用通用接口设置相机参数,包括Int、Float、Enum、Bool等类型节点的设置。

#include <stdio.h>
#include <string.h>
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// 获取int型变量
// get IInteger variable
MVCC_INTVALUE stHeight = {0};
nRet = MV_CC_GetIntValue(handle, "Height", &stHeight);
if (MV_OK == nRet)
{
printf("height current value:%d\n", stHeight.nCurValue);
printf("height max value:%d\n", stHeight.nMax);
printf("height min value:%d\n", stHeight.nMin);
printf("height increment value:%d\n\n", stHeight.nInc);
}
else
{
printf("get height failed! nRet [%x]\n\n", nRet);
}
// 设置int型变量
// set IInteger variable
unsigned int nHeightValue = 0;
printf("please input the height to set:");
scanf("%d", &nHeightValue);
// 宽高设置时需考虑步进(16),即设置宽高需16的倍数
// Step (16) should be considered when setting width and height, that is the width and height should be a multiple of 16
nRet = MV_CC_SetIntValue(handle, "Height", nHeightValue);
if (MV_OK == nRet)
{
printf("set height OK!\n\n");
}
else
{
printf("set height failed! nRet [%x]\n\n", nRet);
}
// 获取float型变量
// get IFloat variable
MVCC_FLOATVALUE stExposureTime = {0};
nRet = MV_CC_GetFloatValue(handle, "ExposureTime", &stExposureTime);
if (MV_OK == nRet)
{
printf("exposure time current value:%f\n", stExposureTime.fCurValue);
printf("exposure time max value:%f\n", stExposureTime.fMax);
printf("exposure time min value:%f\n\n", stExposureTime.fMin);
}
else
{
printf("get exposure time failed! nRet [%x]\n\n", nRet);
}
// 设置float型变量
// set IFloat variable
float fExposureTime = 0.0f;
printf("please input the exposure time to set: ");
scanf("%f", &fExposureTime);
nRet = MV_CC_SetFloatValue(handle, "ExposureTime", fExposureTime);
if (MV_OK == nRet)
{
printf("set exposure time OK!\n\n");
}
else
{
printf("set exposure time failed! nRet [%x]\n\n", nRet);
}
// 获取enum型变量
// get IEnumeration variable
MVCC_ENUMVALUE stTriggerMode = {0};
nRet = MV_CC_GetEnumValue(handle, "TriggerMode", &stTriggerMode);
if (MV_OK == nRet)
{
printf("TriggerMode current value:%d\n", stTriggerMode.nCurValue);
printf("supported TriggerMode number:%d\n", stTriggerMode.nSupportedNum);
for (unsigned int i = 0; i < stTriggerMode.nSupportedNum; ++i)
{
printf("supported TriggerMode [%d]:%d\n", i, stTriggerMode.nSupportValue[i]);
}
printf("\n");
}
else
{
printf("get TriggerMode failed! nRet [%x]\n\n", nRet);
}
// 设置enum型变量
// set IEnumeration variable
unsigned int nTriggerMode = 0;
printf("please input the TriggerMode to set:");
scanf("%d", &nTriggerMode);
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", nTriggerMode);
if (MV_OK == nRet)
{
printf("set TriggerMode OK!\n\n");
}
else
{
printf("set TriggerMode failed! nRet [%x]\n\n", nRet);
}
// 获取bool型变量
// get IBoolean variable
bool bGetBoolValue = false;
nRet = MV_CC_GetBoolValue(handle, "ReverseX", &bGetBoolValue);
if (MV_OK == nRet)
{
if (0 != bGetBoolValue)
{
printf("ReverseX current is true\n\n");
}
else
{
printf("ReverseX current is false\n\n");
}
}
// 设置bool型变量
// set IBoolean variable
int nSetBoolValue;
bool bSetBoolValue;
printf("please input the ReverseX to set(bool): ");
scanf("%d", &nSetBoolValue);
if (0 != nSetBoolValue)
{
bSetBoolValue = true;
}
else
{
bSetBoolValue = false;
}
nRet = MV_CC_SetBoolValue(handle, "ReverseX", bSetBoolValue);
if (MV_OK == nRet)
{
printf("Set ReverseX OK!\n\n");
}
else
{
printf("Set ReverseX Failed! nRet = [%x]\n\n", nRet);
}
// 获取string型变量
// get IString variable
MVCC_STRINGVALUE stStringValue = {0};
nRet = MV_CC_GetStringValue(handle, "DeviceUserID", &stStringValue);
if (MV_OK == nRet)
{
printf("Get DeviceUserID [%s]\n\n", stStringValue.chCurValue);
}
else
{
printf("Get DeviceUserID Failed! nRet = [%x]\n\n", nRet);
}
// 设置string型变量
// set IString variable
unsigned char strValue[256];
printf("please input the DeviceUserID to set(string):");
scanf("%s", strValue);
nRet = MV_CC_SetStringValue(handle, "DeviceUserID", (char*)strValue);
if (MV_OK == nRet)
{
printf("Set DeviceUserID OK!\n\n");
}
else
{
printf("Set DeviceUserID Failed! nRet = [%x]\n\n", nRet);
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}

Trigger_Image.cpp

触发模式获取相机图像

该示例程序说明了触发模式下通过软触发主动取图的方式获取图像。创建句柄,打开相机,设置触发模式,设置触发源(软触发),开始取流,创建取流线程(发送软触发command命令, MV_CC_GetOneFrameTimeout() 取图),停止取流,关闭相机,销毁句柄。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bExit = false;
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(pUser, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
return NULL;
}
MV_FRAME_OUT_INFO_EX stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
unsigned char * pData = (unsigned char *)malloc(sizeof(unsigned char) * stParam.nCurValue);
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize = stParam.nCurValue;
while(1)
{
nRet = MV_CC_SetCommandValue(pUser, "TriggerSoftware");
if(MV_OK != nRet)
{
printf("failed in TriggerSoftware[%x]\n", nRet);
}
nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else
{
printf("Get One Frame failed![%x]\n", nRet);
}
if(g_bExit)
{
break;
}
}
if (pData)
{
free(pData);
pData = NULL;
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
nRet = MV_CC_SetBoolValue(handle, "AcquisitionFrameRateEnable", false);
if (MV_OK != nRet)
{
printf("set AcquisitionFrameRateEnable fail! nRet [%x]\n", nRet);
break;
}
// 设置触发模式为on
// set trigger mode as on
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 1);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
// 设置触发源
// set trigger source
nRet = MV_CC_SetEnumValue(handle, "TriggerSource", MV_TRIGGER_SOURCE_SOFTWARE);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerSource fail! nRet [%x]\n", nRet);
break;
}
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
// 停止取流
// end grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}

Trigger_ImageCallback.cpp

触发模式下通过回调获取相机图像

该示例程序说明了通过软触发回调取图的方式获取图像。创建句柄,打开相机,设置触发模式,设置触发源(软触发),注册抓图回调函数,开始取流,在回调函数中可以获取通过软触发得到的图像信息,停止取流,关闭相机,销毁句柄。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
bool g_bIsGetImage = true;
bool g_bExit = false;
// 等待用户输入enter键来结束取流或结束程序
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (pFrameInfo)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
pFrameInfo->nWidth, pFrameInfo->nHeight, pFrameInfo->nFrameNum);
g_bIsGetImage = true;
}
}
static void* WorkThread(void* pUser)
{
while(1)
{
if(g_bExit)
{
break;
}
if (true == g_bIsGetImage)
{
int nRet = MV_CC_SetCommandValue(pUser, "TriggerSoftware");
if(MV_OK != nRet)
{
printf("failed in TriggerSoftware[%x]\n", nRet);
}
else
{
g_bIsGetImage = false;
}
}
else
{
continue;
}
}
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// 枚举设备
// enum device
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// 选择设备并创建句柄
// select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// 打开设备
// open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
nRet = MV_CC_SetBoolValue(handle, "AcquisitionFrameRateEnable", false);
if (MV_OK != nRet)
{
printf("set AcquisitionFrameRateEnable fail! nRet [%x]\n", nRet);
break;
}
// 设置触发模式为on
// set trigger mode as on
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 1);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
// 设置触发源
// set trigger source
nRet = MV_CC_SetEnumValue(handle, "TriggerSource", MV_TRIGGER_SOURCE_SOFTWARE);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerSource fail! nRet [%x]\n", nRet);
break;
}
// 注册抓图回调
// register image callback
nRet = MV_CC_RegisterImageCallBackEx(handle, ImageCallBackEx, handle);
if (MV_OK != nRet)
{
printf("MV_CC_RegisterImageCallBackEx fail! nRet [%x]\n", nRet);
break;
}
// 开始取流
// start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
// 停止取流
// end grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// 关闭设备
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// 销毁句柄
// destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit\n");
return 0;
}